package com.quick.calcu.controller;

import com.quick.calcu.entity.Teacher;
import com.quick.calcu.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/teachers")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;



    // 根据ID查询教师
    @GetMapping("/{id}")
    public ResponseEntity<Map<String, Object>> getTeacher(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        try {
            Teacher teacher = teacherService.getWithUser(id);
            if (teacher != null) {
                result.put("data", teacher);
                result.put("code", "200");
                result.put("message", "查询成功");
                return ResponseEntity.ok(result);
            } else {
                result.put("success", false);
                result.put("message", "教师不存在");
                return ResponseEntity.notFound().build();
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败：" + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }


    // 根据用户ID查询教师
    @GetMapping("/user/{userId}")
    public ResponseEntity<Map<String, Object>> getByUserId(@PathVariable Long userId) {
        Map<String, Object> result = new HashMap<>();
        try {
            Teacher teacher = teacherService.getByUserId(userId);
            if (teacher != null) {
                result.put("code", "200");
                result.put("message", "查询成功");
                result.put("data", teacher);
                return ResponseEntity.ok(result);
            } else {
                result.put("success", false);
                result.put("message", "未找到对应教师");
                return ResponseEntity.notFound().build();
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败：" + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }

    // 添加教师
    @PostMapping
    public ResponseEntity<Map<String, Object>> addTeacher(@RequestBody Teacher teacher) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = teacherService.save(teacher);
            result.put("success", success);
            result.put("code", "200");
            result.put("data", success);
            result.put("message", success ? "添加成功" : "添加失败");
            return success ? ResponseEntity.ok(result) : ResponseEntity.badRequest().body(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "添加失败：" + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }
}